﻿using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace HuiZe.education.ViewModel
{
    public class AttendClassViewModel : ViewModelBase
    {
        public int totalBK { get; set; }//总量
        public int cloudPageCount { get; set; }
        public int RecycleBinPageCount { get; set; }
        public List<ShareDestination> Destinations { get; set; }
        public List<string> RecycleDelIdList { get; set; }
        public ShareDestination SelectedDestination { get; set; }
        public string searchBkName { get; set; }
        public int pageCount { get; set; }
        public RelayCommand CloudClassRoomCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    //CloudClassRoomView cloudClassRoomView = new CloudClassRoomView();
                    //cloudClassRoomView.ShowDialog();
                    if (isUserAuth("3"))
                    {
                        CloudClassRoomView groupView = new CloudClassRoomView() { DataContext = this };
                        groupView.ShowDialog();
                    }
                }));
            }
        }
        public RelayCommand UploadCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    userUploads();
                }));
            }
        }

        public AttendClassViewModel()
        {
            RecycleDelIdList = new List<string>();
            Destinations = new List<ShareDestination>();
            ShareDestination share = new ShareDestination
            {
                Id = 1,
                Destination = "全网"

            };
            ShareDestination share2 = new ShareDestination
            {
                Id = 2,
                Destination = "本校"
            };
            Destinations.Add(share);
            Destinations.Add(share2);
        }


        public void userUploads()
        {
            string filepath = "";
            string filename = "";
            string filesize = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filepath = dialog.FileName;
                filename = dialog.SafeFileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                filesize = (fileInfo.Length / 1024).ToString();

            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                return;
            }

            ReqUploadModel model = new ReqUploadModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;
            //model.file = filepath;


            string data2 = JsonHelper.ToJson(model);
            var result2 = Upload<ResUploadModel>("/api/common/oss_upload", data2, "上传文件", filepath);
            string attachment_id = "";
            if (result2.Item1)
            {
                attachment_id = result2.Item2.data.attachment_id;
            }
            else
            {
                ShowDialogView(result2.Item2.msg);
                return;
            }

            //ReqGroupUserUploadModel2 pamrm = new ReqGroupUserUploadModel2
            //{
            //    token = HuiZeeducation.Instance.UserInfo.token,
            //    attachment_id = attachment_id,

            //};
            uploadJieyazip pamrm = new uploadJieyazip();
            pamrm.token = HuiZeeducation.Instance.UserInfo.token;
            pamrm.path = result2.Item2.data.url;
            string data = JsonHelper.ToJson(pamrm);


            var result = Request<ReqGroupMainModel>("/api/resources/jieyazip", data, "上传备课包");
            if (result.Item1)
            {
                ShowDialogView("上传备课包成功！");
            }
            else
            {
                ShowDialogView(result.Item2.msg);
            }
        }



        /// <summary>
        /// 云课程分类 
        /// </summary>
        public List<ResCloudclassModel> Getcloudclass()
        {
            ReqBkClassModel model = new ReqBkClassModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResCloudclassModel>>("/api/cloud.cloud_class/index", data, "云课程分类");
            if (result.Item1)
            {
                //ShowDialogView("删除成功!");

                return result.Item2.data;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 云课程章节  
        /// </summary>
        public List<RescloudrRoomItemModel> GetcloudRoom(int id, int page)
        {
            ReqcloudrRoomModel model = new ReqcloudrRoomModel
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                cid = id,
                limit = 14,
                page = page
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<RescloudrRoomModel>("/api/cloud.cloud_room/index", data, "云课程章节");
            if (result.Item1)
            {
                cloudPageCount = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 我的备课列表/回收站
        /// </summary>
        public ResMyBkResourceModel GetMyBkResourceList(string SortType, int pages)
        {
            ReqMyBkResourceModel model = new ReqMyBkResourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                search = searchBkName,
                order_type = SortType,
                limit = 8,
                page = pages,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResMyBkResourceModel>("/api/resources/myBclassList", data, "我的备课列表");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                return result.Item2.data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除备课包
        /// </summary>
        public bool DelBclass(int id)
        {
            if (id <= 0)
            {
                ShowDialogView("请选择要删除的备课资源!");
                return false;
            }

            ReqUpResourceModel model = new ReqUpResourceModel();
            model.token = HuiZeeducation.Instance.UserInfo.token;
            model.id = id;
            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/delBclass", data, "删除备课包");
            if (result.Item1)
            {
                ShowDialogView("删除成功!");
                return true;
            }
            else
            {
                return false;

            }

        }

        /// <summary>
        /// 下载备课包
        /// </summary>
        public void DownBkResource(int Id, string Downpath)
        {
            string path = SaveZip();
            if (string.IsNullOrWhiteSpace(path))
            {
                ShowDialogView("请选择下载路径!");
                return;
            }
            ReqUpResourceModel model = new ReqUpResourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id
            };
            string data = JsonHelper.ToJson(model);


            //var result0 = RequestDown("/api/resources/downZip", data, "下载备课包");

            if (string.IsNullOrWhiteSpace(Downpath.ToString()))
            {
                return;

            }
            var result = RequestFile(Downpath.ToString(), "下载备课包", path);
            if (result)
            {
                ShowDialogView("下载成功!");
                //return result;
            }
            else
            {
                //return "";
            }
        }

        //public bool DelShare(int id)
        //{
        //    if (id <= 0)
        //    {
        //        ShowDialogView("请选择要删除的备课资源!");
        //        return false;
        //    }

        //    ReqUpResourceModel model = new ReqUpResourceModel();
        //    model.token = HuiZeeducation.Instance.UserInfo.token;
        //    model.id = id;
        //    string data = JsonHelper.ToJson(model);
        //    var result = Request<ResDelBkModel>("/api/resources/delShare", data, "取消");
        //    if (result.Item1)
        //    {

        //        ShowDialogView("删除成功!");
        //        return true;
        //    }
        //    else
        //    {
        //        return false;

        //    }

        //}

        /// <summary>
        /// 添加共享/修改共享
        /// </summary>
        public void SetResourcesShare(int Id)
        {
            ReqAddShareModel model = new ReqAddShareModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
                type = SelectedDestination.Id,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/share", data, "添加共享");
            if (result.Item1)
            {
                ShowDialogView("备课包共享成功!");
            }
            else
            {
                ShowDialogView(result.Item2.msg);
            }
        }

        /// <summary>
        /// 备课包一键引用
        /// </summary>
        public void BkResourcesQuote(int Id)
        {

            ReqAddShareModel model = new ReqAddShareModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resourcse/quoteClass", data, "备课包一键引用");
            if (result.Item1)
            {
                ShowDialogView("备课包一键引用成功!");
            }
            else
            {
            }

        }

        /// <summary>
        /// 置顶资源
        /// </summary>
        public bool SetBkResourceWeigh(int Id)
        {
            ReqUpResourceModel model = new ReqUpResourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = Id,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResUpResourceModel>("/api/resources/setWeigh", data, "置顶资源");
            if (result.Item1)
            {
                ShowDialogView("置顶成功!");
                return true;
            }
            else
            {

            }
            return false;
        }

        /// <summary>
        /// 获取上课资源
        /// </summary>
        public List<ResBkBeginClassResListModel> GetClassResourceDetail(string bkId)
        {
            ReqBkClassDetailModel model = new ReqBkClassDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                bid = bkId,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/beikelist", data, "备课资源详情");
            if (result.Item1)
            {
                return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                return null;
            }
        }

        public void BeginClass(string BkClassId)
        {
            BeginClass27 beginClassView = new BeginClass27
            {
                DataContext = this,
                BkId = BkClassId
            };
            //beginClassView.ShowInTaskbar = false;
            //beginClassView.Owner = MainView.Instance;
            beginClassView.Show();

        }



        public void BeginClassExtrend(string Title, string Cat, string BkClassId, string Class)
        {


            BeginClass27 beginClassView = new BeginClass27
            {
                DataContext = this,
                BkId = BkClassId,
                Class=Class,
                Title=Title,
                CatName=Cat
            };
            //beginClassView.ShowInTaskbar = false;
            //beginClassView.Owner = MainView.Instance;
            beginClassView.Show();

        }
        /// <summary>
        /// 上课之前检测是否有未退出
        /// </summary>
        public void classbegins(string bkId)
        {
            ReqBkClassDetailModel model = new ReqBkClassDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/classbegins", data, "备课资源详情");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }
        }

        /// <summary>
        /// 结束上课-
        /// </summary>
        public void endattendclass(string bkId)
        {

            ReqendattendclassModel model = new ReqendattendclassModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/endattendclass", data, "备课资源详情");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }
        }

        /// <summary>
        /// 上课抽答-
        /// </summary>
        public void GetclassRandom()
        {
            ReqendattendclassRandomModel model = new ReqendattendclassRandomModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                grades_id = 0
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/student/Random", data, "备课资源详情");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }
        }

        /// <summary>
        /// 抽答结果-
        /// </summary>
        public void classrandStastu()
        {
            ReqclassRandomResultModel model = new ReqclassRandomResultModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                stulog_id = 0
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/student/randStastu", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }
        }

        ///学生考勤
        ///
        #region
        public void GetstudentList()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                grades_id = 0
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResStudentList>>("/api/student/studentList", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }

        /// <summary>
        /// 	显示考勤状态
        /// </summary>
        public void GetstudentLog()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                grades_id = 0
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResStudentLog>>("/api/student/studentLogList", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }
        /// <summary>
        /// 修改考勤状态
        /// </summary>
        public void EditStudentLog()
        {

            ReqEditStuLog model = new ReqEditStuLog()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                status = 0,
                student_id = 0,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("//api/student/editStudentLog", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }

        /// <summary>
        /// 一键考勤
        /// </summary>
        public void StudentoneCheck()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
                grades_id = 0
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("//api/student/oneCheck", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }

        /// <summary>
        /// 画笔使用记录

        /// </summary>
        public void Getdrawlog()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/drawlog", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }


        /// <summary>
        /// 倒计时使用记录
        /// </summary>
        public void countdownlog()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                log_id = 0,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/countdownlog", data, "抽答结果");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }







        #endregion

        /// <summary>
        /// 回收站列表
        /// </summary>
        public List<ResCallResourceListItemModel> GetResourcesdellist(int pages)
        {

            ReqBkClassModel model = new ReqBkClassModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,

            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResCallResourceListModel>("/api/resources/dellist", data, "回收站列表");
            if (result.Item1)
            {
                RecycleBinPageCount = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                ShowDialogView(result.Item2.msg);
                return null;
            }
        }

        /// <summary>
        /// 回收站恢复
        /// </summary>
        public void GetResourcesdelBack()
        {
            if (RecycleDelIdList.Count <= 0)
            {
                ShowDialogView("请选择要恢复的备课!");
                return;
            }
            string _ids = string.Empty;
            foreach (var id in RecycleDelIdList)
            {
                _ids += id + ",";
            }
            _ids = _ids.Substring(0, _ids.Length - 1);
            ReqdelbackModel model = new ReqdelbackModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                ids = _ids,

            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResDelBkModel>("/api/resources/delback", data, "回收站恢复");
            if (result.Item1)
            {
                ShowDialogView(result.Item2.msg);
            }
            else
            {

            }
        }

        /// <summary>
        /// 回收站恢复/一键清空
        /// </summary>
        public void SetResourcesdelall()
        {
            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "是否一键清空回收站";
            if (showDialogView.ShowDialog() == true)
            {
                ReqBkClassModel model = new ReqBkClassModel();

                model.token = HuiZeeducation.Instance.UserInfo.token;

                string data = JsonHelper.ToJson(model);
                var result = Request<ResDelBkModel>("/api/resources/delall", data, "一键清空");
                if (result.Item1)
                {
                    ShowDialogView("一键清空成功!");
                }
                else
                {
                }
            }
        }

        /// <summary>
        /// 1 iscourse
        /// 2 resoure
        /// 3 yuncource
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool isUserAuth(string type)
        {
            userauthModel model = new userauthModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;
            //model.file = filepath;


            string data2 = JsonHelper.ToJson(model);
            var result2 = Request<userauthRetModel>("/api/index/userauth", data2, "获取用户权限");
            if (result2.Item1)
            {
                if (type == "1")
                {
                    if (result2.Item2.data.is_course == "1")
                    {
                        return true;
                    }
                }
                else if (type == "2")
                {
                    if (result2.Item2.data.is_resource == "1")
                    {
                        return true;
                    }

                }
                else if (type == "3")
                {
                    if (result2.Item2.data.is_yuncource == "1")
                    {
                        return true;
                    }

                }
                else
                {
                    ShowDialogView("当前资源未授权，请联系管理员！");
                }
            }
            else
            {
                ShowDialogView(result2.Item2.msg);
            }
            ShowDialogView("当前资源未授权，请联系管理员！");
            return false;
        }
    }
}
